package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private UserDao userDao;
    @Resource
    private DamageListGoodsDao damageListGoodsDao;
    @Resource
    private DamageListDao damageListDao;
    //保存报损单
    @Override
    public ServiceVO saveDamageListGoods(DamageList damageList, String damageListGoodsStr) {


        // 使用谷歌Gson将JSON字符串数组转换成具体的集合
        Gson gson = new Gson();
        List<DamageListGoods> damageListGoodsList = gson.fromJson(damageListGoodsStr,new TypeToken<List<DamageListGoods>>(){}.getType());

        // 设置当前操作用户
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        damageList.setUserId(currentUser.getUserId());

        // 保存商品报损单信息
        damageListDao.insertDamageList(damageList);

        // 保存商品报损单商品信息
        damageListGoodsList.stream().peek(damageListGoods -> {
            damageListGoods.setDamageListId(damageList.getDamageListId());
            damageListGoodsDao.insertDamageListGoods(damageListGoods);

            // 修改商品库存，状态
            Goods goods = goodsDao.selectById(damageListGoods.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity()-damageListGoods.getGoodsNum());
            goods.setState(2);
            goodsDao.updateById(goods);

        }).collect(Collectors.toList());

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    //报损单查询
    @Override
    public Map<String, Object> getDamageListGoods(String sTime, String eTime) {
        Map<String, Object> map = new HashMap<>();
        List<DamageList> lists = damageListDao.selectDamageList( sTime, eTime);
        lists.stream().peek(damageList -> {
            User userByName = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
            damageList.setUserId(userByName.getUserId());
            damageList.setTrueName(userByName.getTrueName());

        }).collect(Collectors.toList());
        map.put("rows", lists);
        return map;
    }

    @Override
    public Map<String, Object> getGoodsList(Integer damageListId) {

        Map<String, Object> map = new HashMap<>();
        List<DamageListGoods> list = damageListGoodsDao.selectDamageListGoods( damageListId);
        map.put("rows", list);
        return map;
    }
}
